<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\logic\GoodsLogic;
use app\admin\model\Gcategory;
use app\BaseController;
use Symfony\Component\VarDumper\Caster\LinkStub;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;
//商品页
class Goods extends BaseController{
    //商品添加
    public function goodAdd(){
        //添加商品
        if (Request::isPost()){
            //可以是Request::param();
            $post=Request::post();
            $data=GoodsLogic::GoodsAdd($post);
            $res=\app\admin\model\Goods::AddGoodsInfo($data);
            if ($res){
               $this->success('添加成功',url('goodList'));
            }else{
                $this->error('添加失败');
            }
        }else{
            //分类
            $fenlei=GoodsLogic::goodCate();
            View::assign('fenlei',$fenlei);
            //品牌
            $pin=\app\admin\model\Goods::goodsPin();
            View::assign('pin',$pin);
            return View::fetch();
        }
    }
    public function goodList(){
        //where条件封装
        $where=GoodsLogic::setWhere();
        //留值设置
        $data=GoodsLogic::setData();
        View::assign($data);
        View::assign('data',$data);
        //时间上下架排序
        $orderid= Request::param('order');
        $order=GoodsLogic::goodsOrder($orderid);
        //品牌的下拉
        $pin=\app\admin\model\Goods::goodsPin();
        View::assign('pin',$pin);
        //$datas=self::getGoodsListData();
        //查询数据
        $list=\app\admin\model\Goods:: getGoodsList($order,$data,$where);
        //查询字变色
        $list=GoodsLogic::changeSe($list);
        //分类
        $fenlei=GoodsLogic::goodCate();
        View::assign('fenlei',$fenlei);
        //获取分页显示
         $page = $list->render();
        View::assign(['list'=>$list,'page'=>$page]);
         return View::fetch();
    }

//     public static function   getGoodsListData(){
//         $param=Request::param();
//         $datas=[];
//         if (isset($param['keywords']) &&!empty($param['keywords'])){
//            $datas['keywords']=$param['keywords'];
//        }
//        if (isset($param['if_show']) && count($param['if_show'])==1){
//            $datas['if_show']=$param['if_show'][0];
//        }
//        if (isset($param['recommended']) && $param['recommended']<>''){
//            $datas['recommended']=$param['recommended'];
//        }
//        if (isset($param['pinpai']) && !empty($param['pinpai'])){
//            $datas['pinpai']=$param['pinpai'];
//        }
//
//        if (isset($param['mintime']) && !empty($param['mintime'])){
//            $datas['mintime']=$param['mintime'];
//        }
//        if (isset($param['maxtime']) && !empty($param['maxtime'])){
//            $datas['maxtime']=$param['maxtime'];
//        }
//        if (isset($param['minmoney']) && !empty($param['minmoney'])){
//            $datas['minmoney']=$param['minmoney'];
//        }
//        if (isset($param['maxmoney']) && !empty($param['maxmoney'])){
//            $datas['maxmoney']=$param['maxmoney'];
//        }
//        if (isset($param['cid']) && !empty($param['cid'])){
//            $datas['cid']=$param['cid'];
//        }
//         return $datas;
//
//     }


     //商品修改
     public function goodEdit(){
        if (Request::isPost()){
          $post=Request::post();
          $post['data']=Request::get('data');
          $data=GoodsLogic::goodsEditLogic($post);
          $res=\app\admin\model\Goods::goodEditInfo($data,[]);
            if ($res){
                if ($post['btn']==1){
                    $this->success('修改成功',url('goodList',$post['data']));
                }elseif ($post['btn']==0){
                    $this->success('修改成功',url('?id='.$post['gid'],$post['data']));
                }
            }else{
                $this->error('修改失败');
            }
        }else{
            //搜索留值
            $data=GoodsLogic::setData();
            View::assign('data',$data);
            //查询关于主键id的一条数据
            $get=Request::get();
           $find=\app\admin\model\Goods::goodEditInfo([],$get);
            View::assign('find',$find);
            //分类
            $fenlei=GoodsLogic::goodCate();
            View::assign('fenlei',$fenlei);
            //品牌
            $pin=\app\admin\model\Goods::goodsPin();
            View::assign('pin',$pin);
            return View::fetch();

        }



      }












}













